package cn.com.self.apis.member.service.impl;

import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.escrow.mapper.EscrowInfoMapper;
import cn.com.self.apis.files.entity.Files;
import cn.com.self.apis.member.entity.UFileRelation;
import cn.com.self.apis.member.entity.UReportInfo;
import cn.com.self.apis.member.entity.dto.ReportInfoDTO;
import cn.com.self.apis.member.entity.vo.UReportInfoVO;
import cn.com.self.apis.member.mapper.ReportFileMapper;
import cn.com.self.apis.member.mapper.ReportInfoMapper;
import cn.com.self.apis.member.service.IReportInfoService;
import cn.com.self.apis.portal.util.ConstantUtils;
import cn.com.self.apis.portal.util.ToolUtil;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class ReportInfoServiceImpl extends BaseServiceImpl<UReportInfo, String> implements IReportInfoService {
    @Autowired
    private ReportInfoMapper reportInfoMapper;
    @Autowired
    private ReportFileMapper reportFileMapper;
    @Autowired
    private EscrowInfoMapper escrowInfoMapper;
    @Override
    public UReportInfo loadReportInfoById(String id) {
        ArrayList<Files> files = reportInfoMapper.queryReportInfoFiles(id, ConstantUtils.FILE_TYPE_JUBAO);
        UReportInfo uReportInfo = reportInfoMapper.loadReportInfoById(id);
        uReportInfo.setReportInfoFiles(files);
        return reportInfoMapper.loadReportInfoById(id);
    }

    @Override
    public ResponseEntity createReportInfo(ReportInfoDTO requestDto) {
        if (ToolUtil.isEmpty(requestDto.getMemberName())){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"举报会员名称不能为空"));
        }
        if (requestDto.getMemberId() == null){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"举报会员id不能为空"));
        }
        if (ToolUtil.isEmpty(requestDto.getMemberNameEd())){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"被举报会员名称不能为空"));
        }
        if (requestDto.getMemberIdEd() == null){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"被举报会员id不能为空"));
        }
        if (ToolUtil.isEmpty(requestDto.getContent())){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"内容描述不能为空"));
        }
        UReportInfo uReportInfo = new UReportInfo();
        requestDto.setTime(new Date());
        requestDto.setCreateTime(new Date());
        CopyUtils.copyProperties(requestDto,uReportInfo);
        UReportInfo create = this.create(uReportInfo);
        ArrayList<Files> reportInfoFiles = requestDto.getReportInfoFiles();
        if(reportInfoFiles != null){
            for(Files file : reportInfoFiles){
                UFileRelation uFileRelation = new UFileRelation();
                uFileRelation.setFileId(file.getId());
                uFileRelation.setObjId(create.getId());
                uFileRelation.setType(ConstantUtils.FILE_TYPE_JUBAO);
                reportFileMapper.insert(uFileRelation);
            }
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
    }

    @Override
    public UReportInfo updateReportInfo(String id, UReportInfoVO entity) {
        escrowInfoMapper.deleteUFile(id,ConstantUtils.FILE_TYPE_JUBAO);
        UReportInfo reportInfo = this.load(id);
        CopyUtils.copyProperties(entity, reportInfo);
        reportInfo.setUpdateTime(new Date());
        UReportInfo update = this.update(reportInfo);
        ArrayList<Files> reportInfoFiles = entity.getReportInfoFiles();
        if(reportInfoFiles != null){
            for(Files file : reportInfoFiles){
                UFileRelation uFileRelation = new UFileRelation();
                uFileRelation.setFileId(file.getId());
                uFileRelation.setObjId(update.getId());
                uFileRelation.setType(ConstantUtils.FILE_TYPE_JUBAO);
                reportFileMapper.insert(uFileRelation);
            }
        }
        return update;
    }

    @Override
    public void deleteReportInfo(String id) {
        this.delete(id);
        escrowInfoMapper.deleteUFile(id,ConstantUtils.FILE_TYPE_JUBAO);
    }
}
